<template>
    <el-form label-width="80px">
        <el-form-item label="患者名字:">
            {{row.patientName}}
        </el-form-item>
        <el-form-item label="身份证:">
            {{row.patientIdnum}}
        </el-form-item>
        <el-form-item label="诊断描述:">
            <el-input
                    v-model="prescription.prescriptionDiagnosis"
                    :rows="2"
                    type="textarea"
                    placeholder="添加诊断信息"
            />
        </el-form-item>
        <el-form-item label="药品选择:">
            <el-select v-model="prescription.drugs"
                       multiple
                       filterable
                       remote
                       reserve-keyword
                       collapse-tags
                       :remote-method="LoadWarehouseDatas"
                       placeholder="选择药品">
                <el-option
                        v-for="(item,index) in drugsData"
                        :key="index"
                        :label="item.drugsName"
                        :value="item.drugsId"
                >
                </el-option>
            </el-select>
        </el-form-item>
        <el-scrollbar max-height="100px" v-if="prescription.drugs.length!==0">
                <el-form-item v-for="(item,index) in prescription.drugsRest" :key="index" :label="item.drugsName">
                    <el-input-number v-model="item.drugsCount" :min="1" :max="item.drugsMaxCount" />
                </el-form-item>

        </el-scrollbar>
        <el-form-item label="确认操作:">
            <el-button style="width: 100%" @click="addPrescription">
                确认修改
            </el-button>
        </el-form-item>
    </el-form>
</template>

<script>
    import utils from "@/Utils";
    export default {
        name: "DoctorAddPrescription",
        props:{
            row:Object
        },
        data(){
            return{
                prescription:{
                    patientId:this.row.patientId,
                    prescriptionDiagnosis:'',
                    drugs:[],
                    drugsRest:[]
                },
                drugsData:[],
                utils
            }
        },
        methods:{
            LoadWarehouseDatas:async function (search) {
                await this.axios.post("/Drugs/isLogin/getDrugs",{
                    pageIndex:1,
                    pageSize:10,
                    search:search
                }).then((req)=>{
                    const data=req.data;
                    if(data.code>0){
                        this.utils.tipsMessage(data.error,data.code);
                        return
                    }
                    this.drugsData=data.data.pageData;
                })
            },
            addPrescription:function () {
                const pres=this.prescription;
                console.log(pres)
                if (this.utils.isEmpty(pres.prescriptionDiagnosis)) {
                    this.tipsMessage("请不要留空");
                    return;
                }
                if (pres.drugsRest.length === 0) {
                    this.tipsMessage("请不要留空");
                    return;
                }
                this.axios.post("/Prescription/isLogin/isDoctor/addPrescription",{
                    patientId:pres.patientId,
                    prescriptionDiagnosis:pres.prescriptionDiagnosis,
                    drugs:pres.drugsRest
                }).then((req)=>{
                    const data=req.data;
                    if(data.code>0){
                        this.utils.tipsMessage(data.error,data.code);
                        return
                    }
                    this.utils.tipsMessage(data.data,data.code);
                    this.prescription={
                        patientId:this.row.patientId,
                        prescriptionDiagnosis:'',
                        drugs:[],
                        drugsRest:[]
                    }
                });
            },
        },
        watch:{
            'prescription.drugs'(n,o){
                console.log(n,o);
                if (n.length < o.length) {
                    o.forEach(item=>{
                        if (n.indexOf(item) === -1) {
                            console.log(item);
                            const index= this.prescription.drugsRest.findIndex(value => {
                                return value.drugsId===item;
                            });
                            this.prescription.drugsRest.splice(index,1);
                        }
                    })
                }
                if (n.length>o.length){
                    const filter = this.drugsData.filter((value => {
                        return value.drugsId===n[n.length-1];
                    }));
                    if (filter[0].drugsCount === 0) {
                        this.prescription.drugs.pop()
                        this.utils.tipsMessage("当前药物无库存",1)
                        return;
                    }
                    this.prescription.drugsRest.unshift({drugsId:filter[0].drugsId,drugsName:filter[0].drugsName,drugsMaxCount:filter[0].drugsCount,drugsCount:1});
                }
    }
        },
        mounted(){
            this.LoadWarehouseDatas("");
        }
    }
</script>

<style scoped>

</style>